<template>
    <div class="">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-message"></i> 圈子文章列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-button type="primary" style="float:right" round @click="urlAdd" ><i class="el-icon-plus"> </i>&nbsp;&nbsp;发表圈子文章</el-button>
                <div style="height:50px"></div>
                <el-form :inline="true"  class="demo-form-inline">
                    <el-form-item label="文章标题:">
                        <el-input v-model="title" placeholder="请输入圈子文章标题" clearable style="width:300px;"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSelect"> &nbsp;&nbsp;查询&nbsp;&nbsp;  </el-button>
                        <!-- <el-button type="primary" @click="onResetting"> &nbsp;&nbsp;重置&nbsp;&nbsp;  </el-button> -->
                    </el-form-item>
                </el-form>
                 <div style="height:40px"></div>
                <el-table :data="tableData"  style="width: 100%"   ref="multipleTable">
                    <el-table-column prop="id" label="ID" width="100" align="center">
                    </el-table-column>
                    <el-table-column label="封面图" width="300" align="center">
                        <template slot-scope="scope" >
                            <div><img :src="scope.row.head_img" style="width:300px;height:100px"></div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="title" label="标题" width="450" align="center">
                    </el-table-column>
                    <el-table-column prop="cityName" label="所属分站" width="100" align="center">
                    </el-table-column>
                     <el-table-column prop="read_num" label="浏览数" width="100" align="center">
                    </el-table-column>
                      <el-table-column prop="create_time" label="发表时间" width="200" align="center">
                    </el-table-column>
                    <el-table-column label="操作" align="center" >
                        <template slot-scope="scope" >
                            <el-button class="look" size="small" type="text" @click="handleLook(scope.$index, scope.row)">查看</el-button>
                            <el-button class="edit" size="small" type="text" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                            <el-button class="del" size="small" type="text"  @click="handleDelete(scope.$index, scope.row)" >删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination" v-show="totalPages>1">
                    <el-pagination @current-change="handleCurrentChange" layout="prev, pager, next" :page-size="pageSize" :total="totalElements" background>
                    </el-pagination>
                </div>
                
        </div>
        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
        <!-- / 删除提示框 -->
    </div>
</template>
<script>
import {getCityList,getTopicList,postDeleteTopic,postAddTopic,postEditTopic} from '@/api/topic'
export default {
    name:"topicList",
    data() {
        return {
            pageIndex:1,//当前页码
            pageSize:10,//每页10条
            cityData:[],//所有分站城市数据
            totalElements:1,//默认的总条数
            totalPages:1,//默认的总页数
            tableData:[],//表格数据
            delIndex:0, //当前选中的下标
            delId:0,  //当前要删除的Id
            delVisible:false, //默认dialog 不弹出
            title:'', //文章标题
        }
    },
    //实例创建
    created(){
        //获得所有分站城市
        this.getCityList();
    },
    //计算属性
    computed:{

    },
    //方法
    methods: {
        //获得所有分站城市
        getCityList(){
             getCityList(1,99999).then((response)=> {
                // console.log(response);
                if(response.code==200){
                    this.cityData = response.data;
                    // console.log(this.cityData);
                    //获取表格数据
                     this.getDate();
                }else{
                    this.$notify.error({
                        title: '异常',
                        offset: 100,
                        message: response.msg
                    });
                }
            })
        },
        //获取表格数据
        getDate(){
            getTopicList(this.title,this.pageIndex,this.pageSize).then((response)=> {
                console.log(response);
                let _this = this;
                if(response.code==200){
                    var data = response.data;//当前返回的数据
                    this.totalElements = data.total; //总条数
                    this.totalPages = Math.ceil(data.total/this.pageSize);//总页数
                    let oldData = data.list;//列表数据
                    //遍历重组数据
                    let newAry = [];
                    for(let i=0; i<oldData.length; i++){
                        let obj = {};
                        obj.author = oldData[i].author;
                        obj.cityId = oldData[i].city_id;
                        obj.create_time = oldData[i].create_time;
                        obj.head_img = oldData[i].head_img;
                        obj.id = oldData[i].id; 
                        obj.read_num = oldData[i].read_num;
                        obj.title = oldData[i].title;
                       
                        // debugger;
                        //遍历分站城市
                        _this.cityData.forEach(element => {
                            if(element.id == obj.cityId){
                                obj.cityName = element.name;
                             }
                        });
                     
                        newAry.push(obj);
                    }
                    this.tableData = newAry;
                    // console.log("this.tableData",this.tableData);
                }else{
                    this.$notify.error({
                        title: '异常',
                        offset: 100,
                        message: response.msg
                    });
                }
            })
        },
        //发布
        urlAdd(){
            this.$router.push({ path:'/topic/add'})
        },
        //查询
        onSelect(){
            this.pageIndex =1;
            this.getDate();
        },
        //分页
        handleCurrentChange(val){
            this.pageIndex =val;
            this.getDate();
        },
        //查看
        handleLook(index,row){
            //跳转到详情页
              if(row.id && row.id>0){//编辑状态
                this.$router.push({ path: '/topic/details',query:{id:row.id}});
            }else{
                this.$notify.info({title: '提示',message: '获取详情ID失败，请重试！'});
            }              
        },
        //编辑
        handleEdit(index,row){
            if(row.id && row.id>0){
                //跳转到分站编辑
                this.$router.push({ path: '/topic/edit',query:{id:row.id}});
            }
        },
        //删除
        handleDelete(index,row){
            // console.log(index,row.id);
            if(row.id && row.id>0){
                this.delIndex =index;//要删除的下标
                this.delId = row.id; //要删除的ID
                this.delVisible = true; //显示兑换弹出框
                 //console.log(this.delVisible);
            }else{
                // console.log("没触发");
            }
        },
        // 确定删除
        deleteRow(){
            //console.log(this.delId)
            if(this.delId>0){
                console.log(this.delId)
                postDeleteTopic(this.delId).then(response=>{
                    // console.log(response);
                    if(response.code==200){
                        this.tableData.splice(this.delIndex, 1);
                        this.$message.success('删除成功');
                        this.delVisible = false;
                    }else{
                        this.$notify.error({
                            title: '删除失败',
                            duration:2000,
                            message: '失败原因：'+response.data.msg
                        });
                    }
                    
                })
            }
            
        }
        
    }
}
</script>
<style lang="stylus" scoped>
    .edit{
        color #ff9800;
    }
    .del{
        color #e51c23;
    }
    .download{
        color #67C23A;
    }
</style>


